<?php
$startTime = microtime(true);

date_default_timezone_set('Asia/Ho_Chi_Minh');

//disable magic quotes
if (get_magic_quotes_gpc()) {
    function stripslashes_deep($value)
    {
        $value = is_array($value) ?
                    array_map('stripslashes_deep', $value) :
                    stripslashes($value);

        return $value;
    }

    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
    $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}


session_start();
//set_include_path($this->_frameworkPath . DIRECTORY_SEPARATOR . 'libs');

require_once '../../vega.php';

//load constants
require_once 'Vega/Router/Standard.php';

$app->setupApp('bbservice');

require_once APP_PATH.DS.'config/predefined.php';
require_once APP_PATH.DS.'includes/common.php';

//Append local config
$localConfig = $app->loadConfig('local', 'localConfig');
$app->coreConfig = array_merge_recursive($app->coreConfig, $localConfig); 

if($localConfig['webdebug']['enabled'] || isset($_REQUEST[$localConfig['webdebug']['secretkey']]))
{
	require_once 'Vega/Log/WebDebug.php';
	$debugger = Vega_Log_WebDebug::getDebugger();
}

require_once 'Vega/Db/Mysql.php';
$db = new Vega_Db_Mysql($app->coreConfig['database']['master'], $app->coreConfig['database']['slave']);

require_once 'Vega/Dao.php';
Vega_Dao::setDb($db);
Vega_Dao::setLoader($app->getLoader());
Vega_Dao::setLoader(Vega_App::getInstance()->getLoader());

$response = $app->getResponse();
//$response->startCache((isset($_SESSION['userid']) ? $_SESSION['userid'] : 0), $cacheEngine);

require_once 'Vega/View.php';
//Vega_View::setGlobalCacheManager($cacheEngine);

require_once 'Vega/Dispatcher.php';
$dispatcher = new Vega_Dispatcher($app);

try {
    $response = $dispatcher->dispatch();
} catch (Exception $e) {
    switch ($e->getCode()) {
		default:		
			$response->setResponseCode(500);
			//$response->setOutput(file_get_contents('static/offline/index.html'));
			$response->setOutput($e->getMessage());			
			echo $response;
			
            if(isset($localConfig['logging']['error']) && $localConfig['logging']['error']['enabled'])
            {
                require_once 'Vega/Log/Writer.php';
                $log = new Vega_Log_Writer($localConfig['logging']['dir'].DS.$localConfig['logging']['error']['file']);
                $line = sprintf("%s %s", date('d-m-Y H:m:s'), $e->getMessage());
                $log->write($line);
            }
	}
    exit;
}
echo $response;

$cwdCookie = isset($localConfig['cookie_key']) && isset($_COOKIE[$localConfig['cookie_key']]);

if($localConfig['webdebug']['enabled'] || isset($_REQUEST[$localConfig['webdebug']['secretkey']]) || $cwdCookie)
{
	if ($app->getWebDebugConfig())
	{
		$debugger->setConfig($app->coreConfig);
		$debugger->setTime(microtime(true) - $startTime);
		$debugger->show();
	}
}
/**/